package rejjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;
import rejjie.common.R;
import rejjie.dto.ToEmail;
import rejjie.entity.User;
import rejjie.service.UserService;
import rejjie.utils.ValidateCodeUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JavaMailSender mailSender;


    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private UserService userservice;

    /**
     * 生成验证码，并发送邮件
     * @param user
     * @param session
     */
    @PostMapping("/sendMsg")
    public R<String> sendcode(@RequestBody User user, HttpSession session){
        log.info("{}",user.getPhone());
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(from);
        //谁要接收
        message.setTo(user.getPhone());
        //邮件标题
        message.setSubject("验证码！");
        //邮件内容
        String code = ValidateCodeUtils.generateValidateCode4String(4);
        log.info("验证码是{}",code);
        message.setText("你的验证码是"+ code);

        // 把验证码放入session中
        session.setAttribute(user.getPhone(), code);
        mailSender.send(message);

        return R.success("验证码发送成功");

    }

    /**
     * 前端穿过来的数据是 json形式的，所以后台可以使用Map进行接受 但是一定要@ReqeustBody 进行标注
     * 方法流程
     *  1. 看用户是否存在 user表中，如果存在 直接看code是非正确
     *  2. 如果用户不在 user表中，我们对他进行直接创建 相当于直接注册一个用户
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<String> checklogin(@RequestBody Map map,HttpSession session){
        String phone = (String)map.get("phone");
        String code = (String)map.get("code");

        log.info("{},{}",phone,code);
        String codeInsession =(String)session.getAttribute(phone);
        if(code!=null&& codeInsession.equals(code)){
            log.info("验证码验证正确");

            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userservice.getOne(userLambdaQueryWrapper);

            if(user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userservice.save(user);
            }
            session.setAttribute("user",user.getId());
            return R.success("登陆成功");
        }
        return R.error("登录失败");
    }


}
